package com.matrix.index.ui.home.fragment

import android.view.LayoutInflater
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.matrix.basecore.arouter.ARouterUtils
import com.matrix.basecore.base.BaseFragment
import com.matrix.basecore.constant.Constant
import com.matrix.basecore.utils.SpacesItemDecoration
import com.matrix.basecore.utils.glide.GlideUtil
import com.matrix.index.R
import com.matrix.index.databinding.FragmentGalleryBinding
import com.matrix.index.net.IndexResult
import com.matrix.index.ui.home.adapter.RVWallpaperAdapter
import com.matrix.index.ui.home.bean.SearchBean
import com.matrix.index.ui.home.bean.WallpaperBean
import com.matrix.index.ui.home.viewmodel.WallpaperHotVM

class GalleryFragment : BaseFragment<FragmentGalleryBinding>() {

    private val vm: WallpaperHotVM by viewModels()
    private val HOT: String = "hot"
    private val NEW: String = "new"
    private var type: String = HOT
    private var skip: Int = 0
    private lateinit var adapter: RVWallpaperAdapter
    private var verticals: MutableList<WallpaperBean.Vertical> = mutableListOf()

    override fun getViewBinding(): FragmentGalleryBinding {
        return FragmentGalleryBinding.inflate(layoutInflater)
    }

    override fun initView(inflater: LayoutInflater) {
        var glm = GridLayoutManager(context, 3)
        mViewBinding.recyclerview.layoutManager = glm
        adapter = RVWallpaperAdapter(R.layout.wallpaper_gallery_layout, verticals)
        mViewBinding.recyclerview.addItemDecoration(SpacesItemDecoration(8, 8, 8, 8))
        mViewBinding.recyclerview.adapter = adapter
        GlideUtil.loadRoundImage(
            R.mipmap.icon_phone_wallpaper,
            context,
            mViewBinding.ivPhone,
            8,
            R.mipmap.icon_default_bg
        )
        GlideUtil.loadRoundImage(
            R.mipmap.icon_computer_wallpaper,
            context,
            mViewBinding.ivComputer,
            8,
            R.mipmap.icon_default_bg
        )
    }

    override fun initData() {
        type = NEW
        getWallpaper(type)
    }

    private fun getWallpaper(type: String) {
        vm.getHotNewWallpaper(skip, type).observe(this) { value ->
            if (value.code == IndexResult.SUCCESS) {
                value.t?.let {
                    verticals.addAll(it as List<WallpaperBean.Vertical>)
                    adapter.setList(verticals)
                    adapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInBottom);
                    adapter.loadMoreModule.loadMoreComplete()
                    mViewBinding.smart.finishRefresh()
                    if (it.size < 12) {
                        adapter.loadMoreModule.loadMoreEnd()
                    }
                    saveToDB(verticals)
                }
            } else {
                ToastUtils.showShort(value.msg)
            }
        }
    }

    private fun saveToDB(verticals: MutableList<WallpaperBean.Vertical>) {
        for (item in verticals){
            var db = SearchBean(SearchBean.WALL)
            db.imageDesc = item.tag.toString()
            db.imageUrl = item.img
            db.timestamp = System.currentTimeMillis().toString()
            db.saveOrUpdate("imageUrl = ?", item.img)
        }
    }

    override fun initListener() {
        mViewBinding.ivPhone.setOnClickListener {
            ARouterUtils.startPhoneComputerActivity(Constant.Value.PHONE)
        }
        mViewBinding.ivComputer.setOnClickListener {
            ARouterUtils.startPhoneComputerActivity(Constant.Value.COMPUTER)
        }

        mViewBinding.tvHot.setOnClickListener {
            skip = 0
            type = HOT
            verticals.clear()
            adapter.setList(verticals)
            getWallpaper(type)
            requireContext().run {
                mViewBinding.tvHot.setTextColor(
                    ContextCompat.getColor(
                        this,
                        com.matrix.basecore.R.color.color_F55757
                    )
                )
                mViewBinding.tvNew.setTextColor(
                    ContextCompat.getColor(
                        this,
                        com.matrix.basecore.R.color.black_85
                    )
                )
            }
        }

        mViewBinding.tvNew.setOnClickListener {
            skip = 0
            type = NEW
            verticals.clear()
            adapter.notifyDataSetChanged()
            getWallpaper(type)
            requireContext().run {
                mViewBinding.tvNew.setTextColor(
                    ContextCompat.getColor(
                        this,
                        com.matrix.basecore.R.color.color_F55757
                    )
                )
                mViewBinding.tvHot.setTextColor(
                    ContextCompat.getColor(
                        this,
                        com.matrix.basecore.R.color.black_85
                    )
                )
            }
        }

        mViewBinding.smart.setOnRefreshListener {
            skip = 0
            verticals.clear()
            adapter.notifyDataSetChanged()
            getWallpaper(type)
            mViewBinding.smart
        }

        adapter.loadMoreModule.setOnLoadMoreListener {
            skip += 12
            getWallpaper(type)
        }

        adapter.setOnItemClickListener { _, _, position ->
            run {
                ARouterUtils.startPreviewActivityPath(transitionData(verticals), position)
            }
        }
    }

    private fun transitionData(
        verticals: MutableList<WallpaperBean.Vertical>,
    ): ArrayList<String> {
        var list: ArrayList<String> = arrayListOf()
        if (verticals.size > 0) {
            for (item in verticals) {
                list.add(item.preview)
            }
        }
        return list
    }
}